package com.dgut.Service.impl;

import com.dgut.dao.IRoleRepository;
import com.dgut.dao.IUserRepository;
import com.dgut.domain.IRole;
import com.dgut.domain.IUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;

import java.util.List;

@Service("iUserDetailService")
public class IUserDetailService implements UserDetailsService {

    @Autowired
    IUserRepository iUserRepository;

    @Autowired
    IRoleRepository iRoleRepository;


    public void test(){
        System.out.println(iUserRepository.findByusername("cmj"));
    }

    @Override
    public UserDetails loadUserByUsername(String name) throws UsernameNotFoundException {
        IUser iUser = iUserRepository.findByusername(name);
        if(iUser == null){
            throw new UsernameNotFoundException("用户名不存在");
        }
        BCryptPasswordEncoder praser =new BCryptPasswordEncoder();
        iUser.setPassword(praser.encode(iUser.getPassword()));
        IRole role = iRoleRepository.findByUserID(iUser.getUser_uuid());

        iUser.setiRole(role);
        return iUser;
    }
}
